package com.lanyuan.a.controller;


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lanyuan.a.atest.FoodFormMap;
import com.lanyuan.a.bean.Food;
import com.lanyuan.a.service.ButtonService;
import com.lanyuan.a.service.FoodService;
import com.lanyuan.controller.index.BaseController;
import com.lanyuan.exception.SystemException;
import com.lanyuan.plugin.PageView;
import com.lanyuan.util.Common;

@Controller
@RequestMapping("/food/")
public class FoodController extends BaseController {
	@Autowired
	FoodService foodService;
	@Autowired
	ButtonService buttonService;
	
	@RequestMapping("list")
	public String listUI(Model model) throws Exception {
		model.addAttribute("res", foodService.list());
		model.addAttribute("buttons", buttonService.getButtons("#food"));
		return Common.BACKGROUND_PATH + "/system/food/list";
	}

	@ResponseBody
	@RequestMapping("findByPage")
	public PageView findByPage(String pageNow,String pageSize) throws Exception {
		FoodFormMap foodFormMap = getFormMap(FoodFormMap.class);
		foodFormMap=toFormMap(foodFormMap, pageNow, pageSize);
		List<Food> list = foodService.queryByPage(foodFormMap);
		pageView.setRecords(list);//不调用默认分页,调用自已的mapper中findUserPage
        return pageView;
	}

	@RequestMapping("addUI")
	public String addUI(Model model) throws Exception {
		return Common.BACKGROUND_PATH + "/system/food/add";
	}

	@ResponseBody
	@RequestMapping("addEntity")
	@Transactional(readOnly=false)//需要事务操作必须加入此注解
	public String addEntity(String txtGroupsSelect){
		try {
			System.out.println("addEntity");
			FoodFormMap foodFormMap = getFormMap(FoodFormMap.class);
			String name = (String)foodFormMap.get("name");
			String cost = (String)foodFormMap.get("cost");
			String price = (String)foodFormMap.get("price");
			Food food = new Food();
			food.setName(name);
			food.setCost(Double.parseDouble(cost));
			food.setPrice(Double.parseDouble(price));
			foodService.saveFood(food);
		} catch (Exception e) {
			 throw new SystemException("添加账号异常");
		}
		return "success";
	}

	@ResponseBody
	@RequestMapping("deleteEntity")
	@Transactional(readOnly=false)//需要事务操作必须加入此注解
	public String deleteEntity() throws Exception {
		String[] ids = getParaValues("ids");
		for(String a : ids){
			String[] arr = a.split(",");
			foodService.deleteFoodByIds(arr);
		}
	
		return "success";
	}

	@RequestMapping("editUI")
	public String editUI(Model model) throws Exception {
		String id = getPara("id");
		if(Common.isNotEmpty(id)){
			model.addAttribute("food", foodService.findFoodById(Integer.parseInt(id)));
		}
		return Common.BACKGROUND_PATH + "/system/food/edit";
	}

	@ResponseBody
	@RequestMapping("editEntity")
	@Transactional(readOnly=false)//需要事务操作必须加入此注解
	public String editEntity(Model model , String txtGroupsSelect) throws Exception {
		FoodFormMap foodFormMap = getFormMap(FoodFormMap.class);
		foodFormMap.put("txtGroupsSelect", txtGroupsSelect);
		String id = (String)foodFormMap.get("id");
		String name = (String)foodFormMap.get("name");
		String cost = (String)foodFormMap.get("cost");
		String price = (String)foodFormMap.get("price");
		Food food = foodService.findFoodById(Integer.parseInt(id));
		food.setName(name);
		food.setCost(Double.parseDouble(cost));
		food.setPrice(Double.parseDouble(price));
		foodService.updateFood(food);
		
		return "success";
	}
	/**
	 * 验证账号是否存在
	 * 
	 * @author lanyuan Email：mmm333zzz520@163.com date：2015-2-19
	 * @param name
	 * @return
	 */
	@RequestMapping("isExist")
	@ResponseBody
	public boolean isExist(String name) {
		long n = foodService.findFoodByName(name);
		if (n < 1) {
			return true;
		} else {
			return false;
		}
	}
}